package com.ruoyi.project.task.mission.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.person.dept.domain.PersonDept;
import com.ruoyi.project.person.dept.service.IPersonDeptService;
import com.ruoyi.project.person.info.domain.PersonInfo;
import com.ruoyi.project.person.info.domain.PersonInfoVo;
import com.ruoyi.project.person.info.service.IPersonInfoService;
import com.ruoyi.project.task.mission.domain.Result;
import com.ruoyi.project.task.mission.domain.TaskMission;
import com.ruoyi.project.task.mission.domain.TaskMissionVo;
import com.ruoyi.project.task.mission.service.ITaskMissionService;
import com.ruoyi.project.task.transfer.domain.TaskTransfer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Pathosis
 * @date 2020-09-17 10:35
 * @description: 任务创建页面控制器
 */
@Controller
@RequestMapping("/task/mission")
public class TaskMissionController extends BaseController{

    @Autowired
    private ITaskMissionService itms;

    @Autowired
    private IPersonDeptService ipds;

    @Autowired
    private IPersonInfoService ips;

    private String prefix = "task/mission";

    @RequiresPermissions("task:mission:view")
    @GetMapping
    public String mission(){
        return prefix +"/mission";
    }

    @RequiresPermissions("task:mission:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskMissionVo taskMissionVo)
    {
        //分页
        startPage();

       List<TaskMissionVo> list = itms.selectTaskMissionList(taskMissionVo);

        return getDataTable(list);
    }

    @GetMapping("/add")
    public String missionAdd(ModelMap mmap){
        selectPerAndDept(mmap);
        return prefix + "/add";
    }


    /**
     * 新增保存任务
     */
    @RequiresPermissions("task:mission:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TaskMission taskMission,String endTime)
        {
            return toAjax(itms.insertTaskMissionVo(taskMission,endTime));
    }


    /**
     * 删除任务
     */
    @RequiresPermissions("task:mission:remove")
    @Log(title = "任务删除", businessType = BusinessType.INSERT)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeSave(String ids)
    {
        try
        {
            return toAjax(itms.deleteTaskMissionByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


    /**
     * 回显需要修改的任务
     */
    @GetMapping("/edit/{missionId}")
    public String edit(@PathVariable("missionId") Long missionId, HttpSession session,ModelMap mmap)
    {
        System.out.println( itms.selectTaskMissionById(missionId));
        TaskMission taskMission = itms.selectTaskMissionById(missionId);

        PersonInfo personInfo = ips.selectPersonInAndDept(taskMission.getInfoId());

        session.setAttribute("taskMission",  taskMission);
        mmap.addAttribute("taskMission", taskMission);

        mmap.addAttribute("personInfo", personInfo);

        selectPerAndDept(mmap);

        return prefix + "/edit";
    }


    /**
     * 修改任务
     */
    @RequiresPermissions("task:mission:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TaskMission taskMission,HttpSession session)
    {
        TaskMission tm = (TaskMission) session.getAttribute("taskMission");
        if(!tm.getMissionTitle().equals(taskMission.getMissionTitle())){
            if (UserConstants.POST_NAME_NOT_UNIQUE.equals(itms.checkMissionTitleUnique(taskMission)))
            {
                return error("修改任务'" + taskMission.getMissionTitle() + "'失败，任务标题已存在");
            }
        }
        return toAjax(itms.updateTaskMission(taskMission));
    }

    /**
     * 校验任务标题名称
     */
    @PostMapping("/checkMissionTitleUnique")
    @ResponseBody
    public String checkMissionTitleUnique(TaskMission tmv)
    {
        return itms.checkMissionTitleUnique(tmv);
    }

    /**
     * 校验任务执行人
     */
    @PostMapping("/checkInfoNameUnique")
    @ResponseBody
    public String checkInfoNameUnique(TaskMissionVo tmv)
    {
        return itms.checkInfoNameUnique(tmv);
    }

    @PostMapping("/selectOneTaskMission")
    @ResponseBody
    public TaskMissionVo selectOneTaskMission(Long missionId){
        TaskMissionVo taskMissionVo = itms.selectTaskMissionVoById(missionId);
        return taskMissionVo;

    }

    private void selectPerAndDept(ModelMap mmap){
        //查询出所有的员工
        List<PersonInfo> personInfos = itms.selectAllPerson();
        mmap.addAttribute("list",personInfos );
        //查询出所有部门
        PersonDept pd = new PersonDept();
        List<PersonDept> personDepts = ipds.selectPersonDeptList(pd);
        mmap.addAttribute("personDepts",personDepts );
    }

    @PostMapping("/selectPersomInfo")
    @ResponseBody
    public List<PersonInfo> resultPersultInfo(Long deptId){
        List<PersonInfo> personInfos = ips.selectPersonInfoByDeptId(deptId);
        return personInfos;
    }


}
